package com.bc.web.controller.admin;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.bc.web.config.SystemEnum;
import com.bc.web.model.PayOrder;
import com.bc.web.model.PayOrderItem;
import com.fs.common.extend.FSController;
import com.fs.common.extend.FSSearchCondition;

public class PayOrderContoller extends FSController {

	public void search() {
		FSSearchCondition sc = super.getSearchCondition();
		setPageResultToRequest("items", PayOrder.dao.search(sc));
		render("order/merchPayList.html");
	}

	public void searchItem() {
		String batchNo = getPara("batchNo");
		List<PayOrderItem> items = PayOrderItem.dao.selectByBatchNo(batchNo);
		PayOrder order = PayOrder.dao.selectByBatchNo(batchNo);
		setAttr("order", order);
		setAttr("items", items);
		render("order/merchPayItemList.html");
	}
	
	/** 下载报表 */
	public void download() throws FileNotFoundException, IOException {
		//MerchUser merchUser = (MerchUser) getSession().getAttribute("merchUser");
		String batchNo = getPara("batchNo");

		// 批次
		//PayOrder order = PayOrder.dao.selectByBatchNo(batchNo);
		// 明细
		List<PayOrderItem> items = PayOrderItem.dao.selectByBatchNo(batchNo);
		
		// Workbook wb = new XSSFWorkbook();
		Workbook wb = new HSSFWorkbook();
		Sheet sheet = wb.createSheet("sheet1");
		// 标题行
		Row titleRow = sheet.createRow(0);
		titleRow.createCell(0, CellType.STRING).setCellValue("金额(元)");
		titleRow.createCell(1, CellType.STRING).setCellValue("户名");
		titleRow.createCell(2, CellType.STRING).setCellValue("银行卡号");
		titleRow.createCell(3, CellType.STRING).setCellValue("开户银行名称");
		titleRow.createCell(4, CellType.STRING).setCellValue("开户银行行号");
		titleRow.createCell(5, CellType.STRING).setCellValue("收款方类型");
		titleRow.createCell(6, CellType.STRING).setCellValue("状态");
		titleRow.createCell(7, CellType.STRING).setCellValue("创建时间");
		titleRow.createCell(8, CellType.STRING).setCellValue("响应信息");
		// 数据行
		DecimalFormat df = new DecimalFormat("0.00");
		for (int i = 0; i < items.size(); i++) {
			PayOrderItem item = items.get(i);
			Row row = sheet.createRow(i+1);
			row.createCell(0, CellType.STRING).setCellValue(df.format(item.getAmount()/100.0));
			row.createCell(1, CellType.STRING).setCellValue(item.getAccountName());
			row.createCell(2, CellType.STRING).setCellValue(item.getAccountNumber());
			row.createCell(3, CellType.STRING).setCellValue(item.getBankName());
			row.createCell(4, CellType.STRING).setCellValue(item.getRemark());
			row.createCell(5, CellType.STRING).setCellValue(item.getIsCompany()?"企业":"个人");
			if ("CO".equals(item.getOrderStatus())) {
				row.createCell(6, CellType.STRING).setCellValue("待确认");
			} else if ("IP".equals(item.getOrderStatus())) {
				row.createCell(6, CellType.STRING).setCellValue("交易处理中");
			} else if ("TS".equals(item.getOrderStatus())) {
				row.createCell(6, CellType.STRING).setCellValue("交易成功");
			} else if ("TF".equals(item.getOrderStatus())) {
				row.createCell(6, CellType.STRING).setCellValue("交易失败");
			} else {
				row.createCell(6, CellType.STRING).setCellValue(item.getOrderStatus());
			} 
			row.createCell(7, CellType.STRING).setCellValue(item.getCreateDateTime());
			row.createCell(8, CellType.STRING).setCellValue(item.getRespCode()+"-"+item.getRespMsg());
		}
		String tmpPath = SystemEnum.FilePath.ReportTmp.getValue();
		File file  = new File(tmpPath + batchNo + "代付结果.xls");
		try  (OutputStream fileOut = new FileOutputStream(file)) {
	        wb.write(fileOut);
		}
		wb.close();
		renderFile(file);
	}

}
